package cores

import (
	"github.com/shopspring/decimal"
	"simulationFulu/dao"
	"simulationFulu/models"
	"strconv"
	"time"
)

// 创建一张订单
func CreateOrder(product *models.Product,num int,customerno string) *models.ProOrder{
	prooder:=&models.ProOrder{}
	if product==nil{
		panic("找不到对应产品"+strconv.Itoa(product.Id))
	}
	prooder.OrderId=time.Now().UnixNano()
	NewCards(num,prooder.OrderId,product.HasCardNo,product.HasCardPwd,product.HasCardUrl)
	cards:=dao.CardsByOrderId(prooder.OrderId)
	cardids:=""
	for _,v:=range cards{
		if len(cardids)>0{
			cardids+="-"
		}
			cardids+=strconv.Itoa(v.Id)
	}
	prooder.OperatorSerialNumber=prooder.OrderId
	prooder.OrderState="success"
	prooder.ProductId=product.Id
	prooder.ProductName=product.ProductName
	prooder.CreateTime=time.Now().Format("2006-01-02 15:04:05")
	prooder.FinishTime=time.Now().Format("2006-01-02 15:04:05")
	prooder.BuyNum=num
	prooder.CustomerOrderNo=customerno
	v:=product.ProductPrice.Decimal.Mul(decimal.NewFromInt(int64(num)))
	prooder.OrderPrice=decimal.NullDecimal{Decimal:v,Valid:true}
	prooder.OrderType="4" //直充
	prooder.Cards=cards
	prooder.CardIds=cardids
	dao.ProOrderAdd(prooder)
	return prooder
}

func OrderQueryByCustomerNo(customerNo string) *models.OrderQueryRes{
	res:=&models.OrderQueryRes{}
	order:=dao.ProOrderByCustomerNo(customerNo)
	res.OrderId=order.OrderId
	res.OperatorSerialNumber=order.OperatorSerialNumber
	res.OrderState=order.OrderState
	res.ProductName=order.ProductName
	res.CreateTime=order.CreateTime
	res.OrderPrice=order.OrderPrice
	res.OrderType=order.OrderType
	res.CustomerOrderNo=order.CustomerOrderNo
	res.BuyNum=order.BuyNum
	res.ProductId=order.ProductId
	res.Cards=order.Cards
	res.Area=order.Area
	res.ChargeAccount=order.ChargeAccount
	res.CostType=order.CostType
	res.FinishTime=order.FinishTime
	res.Server=order.Server
	return res
}